package com.dropdb.demo.typehandler;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;

@MappedTypes(Integer[].class)
public class AdminRoleIdsTypehandler extends BaseTypeHandler<Integer[]> {
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Integer[] integers, JdbcType jdbcType) throws SQLException {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");

        //排序
        Arrays.parallelSort(integers);

        for (Integer id : integers) {
            stringBuilder.append(id).append(",");
        }
        stringBuilder.replace(stringBuilder.length() - 1, stringBuilder.length(), "]");
        String s = stringBuilder.toString();
        if (s.length() < 3) {
            s = "";
        }
        preparedStatement.setString(i, s);
    }

    @Override
    public Integer[] getNullableResult(ResultSet resultSet, String s) throws SQLException {
        return parse(resultSet.getString(s));
    }

    @Override
    public Integer[] getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return parse(resultSet.getString(i));
    }

    @Override
    public Integer[] getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        return parse(callableStatement.getString(i));
    }

//    @Override
//    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Admin admin, JdbcType jdbcType) throws SQLException {
//        Integer[] roleIds = admin.getRoleIds();
//        StringBuilder stringBuilder = new StringBuilder();
//        stringBuilder.append("[");
//
//        //排序
//        Arrays.parallelSort(roleIds);
//
//        for (Integer id : roleIds) {
//            stringBuilder.append(id).append(",");
//        }
//        stringBuilder.replace(stringBuilder.length() - 1, stringBuilder.length(), "]");
//        preparedStatement.setString(i, stringBuilder.toString());
//    }
//
//    @Override
//    public Admin getNullableResult(ResultSet resultSet, String s) throws SQLException {
//        Admin admin = pop(resultSet);
//        Integer[] ids = parse(resultSet.getString(s));
//        admin.setRoleIds(ids);
//        return admin;
//    }
//
//    @Override
//    public Admin getNullableResult(ResultSet resultSet, int i) throws SQLException {
//        Admin admin = pop(resultSet);
//        Integer[] ids = parse(resultSet.getString(i));
//        admin.setRoleIds(ids);
//        return admin;
//    }
//
//    @Override
//    public Admin getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
//        Admin admin = pop(callableStatement);
//        Integer[] ids = parse(callableStatement.getString(i));
//        admin.setRoleIds(ids);
//        return admin;
//    }
//
    public Integer[] parse(String value) {
        if (value.length() > 2) {
            String tmp = value.substring(1, value.length() - 1);
            String[] ids = tmp.split("[,]");
            Integer[] frontIds = new Integer[ids.length];
            for (int i = ids.length - 1; i >= 0; i--) {
                frontIds[i] = Integer.valueOf(ids[i]);
            }
            return frontIds;
        }
        return new Integer[]{};
    }
//
//    private Admin pop(ResultSet resultSet) throws SQLException {
//        Admin admin = new Admin();
//        admin.setId(resultSet.getInt("id"));
//        admin.setAddTime(resultSet.getDate("add_time"));
//        admin.setLastLoginTime(resultSet.getDate("last_login_time"));
//        admin.setLastLoginIp(resultSet.getString("last_login_ip"));
//        admin.setPassword(resultSet.getString("password"));
//        admin.setAvatar(resultSet.getString("avatar"));
//        admin.setUsername(resultSet.getString("username"));
//        admin.setDeleted(resultSet.getBoolean("deleted"));
//        admin.setUpdateTime(resultSet.getDate("update_time"));
//        return admin;
//    }
//
//    private Admin pop(CallableStatement clst) throws SQLException {
//        Admin admin = new Admin();
//        admin.setId(clst.getInt("id"));
//        admin.setAddTime(clst.getDate("add_time"));
//        admin.setLastLoginTime(clst.getDate("last_login_time"));
//        admin.setLastLoginIp(clst.getString("last_login_ip"));
//        admin.setPassword(clst.getString("password"));
//        admin.setAvatar(clst.getString("avatar"));
//        admin.setUsername(clst.getString("username"));
//        admin.setDeleted(clst.getBoolean("deleted"));
//        admin.setUpdateTime(clst.getDate("update_time"));
//        return admin;
//    }
}
